// pages/InviteDetail/inviteDetail.js
var network = require("../../utils/NetWorkUtils.js")
var WxParse = require('../../wxParse/wxParse.js');
var app=getApp();
var prizeId=0;
var store=0;
Page({

  /**
   * 页面的初始数据
   */
  data: {
    imageList:[],//兑换人的头像
    title: "",//商品名称,
    exchageGold: 0,//需要多少金币兑换
    exchangeRate: 0,//兑换人数
    headImage: "",//头部图片
    advertisementBanner:"",//底部广告图
    inviteNum:25,//需要邀请多少人
    isShowDown: true,//乡下的箭头
    helpList:[],//邀请人数列表
    Num:0,//人数
    inviteSuccess:false,//邀请人士是否达到
    jump_object: '',//bannerurl
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    prizeId = options.id;
    this.requestDataDetail(options.id)
    this.requestButtomBanner()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function (option) {
    console.log(option.from)
    if(option.from=='button'){
      wx.showShareMenu({
        withShareTicket: true
      })
      return {
        title: '我正在答题赚钱，免费兑换商品，快来和我一起抢！',
        path: 'pages/index/index?openid=' + app.globalData.openId + "&prizeId=" + prizeId,
        imageUrl: '../../images/share_pic.jpg',
      }
    }else{
      wx.showShareMenu({
        withShareTicket: true
      })
      return {
        title: '我正在答题赚钱，免费兑换商品，快来和我一起抢！',
        path: 'pages/index/index',
        imageUrl: '../../images/share_pic.jpg',
      }
    }
 
  },

  getFormID: function (e) {
    console.log(e)
    if (!e.detail.formId||e.detail.formId == "the formId is a mock one"){
        return;
    }
    var dataParam = {
      form_id: e.detail.formId
    }
    network.requestHttp("m=api&c=Index&a=addformid", dataParam, function (res) {
      console.log(res)
    })
  },

//展开
  openMore:function(){
   
    this.setData({
      isShowDown : !this.data.isShowDown,
    })
    console.log(this.data.isShowDown)
  },
//兑换记录
  exchangeRecords:function(){
    wx.navigateTo({
      url: '../member/index?id=' + prizeId,
    })
  },

  freeExchange:function(){
    console.log(store)
    if(store>0){
      this.requestExchangePrize();
    }else{
      wx.showToast({
        title: '该商品已售罄',
        icon: 'none',
        duration: 3000
      })
    }
    
  },

  inviteExchange:function(){
    // wx.showToast({
    //   title: "邀请人数未达到",
    //   icon: 'none',
    //   duration: 3000//持续的时间
    // })
    // return;
  },


  //请求详情数据
  requestDataDetail: function (id) {
    var that = this;
    var requestData = {
      id: id
    }
    network.requestHttp("m=api&c=Index&a=helpdetail", requestData, function (res) {
      console.log(res)

      store = res.data.data.store
      //设置商品介绍 
      var article = res.data.data.detail;
      WxParse.wxParse('article', 'html', article, that, 5);
      var currInviteNum = res.data.data.help.length;//已经邀请的人数
      var needInviteNum = res.data.data.spread_require - currInviteNum;//还需要邀请多少人
      var helpList = res.data.data.help;//邀请人数list
      if (needInviteNum>0){
      for (var i = 0; i < needInviteNum; i++) {//没有邀请的人数,全部添加为空
        helpList.push({ headimgurl: "" })
        }
      }
      if (needInviteNum<=0){
        needInviteNum = res.data.data.spread_require;
        that.setData({
          inviteSuccess:true
        })
      }
      console.log(needInviteNum)
     that.setData({
       imageList: res.data.data.order,
       headImage: res.data.data.pic,
       title: res.data.data.title,
       exchageGold: res.data.data.coin_require,
       exchangeRate: res.data.data.count,
       inviteNum: needInviteNum,
       helpList: helpList,
       Num: res.data.data.spread_require,
     }) 

      console.log(res)
      // //设置商品介绍 
      // var article = res.data.data.detail;
      // WxParse.wxParse('article', 'html', article, that, 5);

    })
  }, 
   /**
   * 广告
   */
  requestButtomBanner: function () {
    var that = this;
    var requestData = { position: 3 }
    network.requestHttp("m=api&c=Index&a=banner", requestData, function (res) {
      console.log(res)
      that.setData({
        advertisementBanner: res.data.data.pic,
        jump_object: res.data.data
      })
    })
  },

  //邀请完成兑换奖品
  requestExchangePrize:function(){
    var dataParam={
      id: prizeId
    }
    network.requestHttp("m=api&c=Index&a=helpget",dataParam,function(res){
      console.log(res)
      if (res.data.msg == 'ok') {//兑换成功跳转
        wx.redirectTo({
          url: '../exchangeSuccess/index?from=invite',
        })
      }else{
        wx.showToast({
          title: res.data.msg,
          icon: 'none',
          duration: 3000//持续的时间
        })
        return;
      }
      
    })
  },
  //底部banner跳转
  banner_jump() {
    var dataParam = {
      id: 3
    }
    network.requestHttp("m=api&c=Index&a=clicked", dataParam, function (res) {

    })
    var that = this;
    if (that.data.jump_object.jump == '2') {
      wx.navigateToMiniProgram({
        appId: that.data.jump_object.appid,
        path: that.data.jump_object.path
      })
    } else {
      wx.navigateTo({
        url: '../webView/indexWebView?bannerUrl=' + that.data.jump_object.jump_url
      })
    }
  },
})